﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Foto
namespace Distrackting.DataLayer
{
    public partial class Foto
    {
        public void setFoto(Foto f)
        {
            this.cod_ponto = f.cod_ponto;
            this.url = f.url;
            this.cod_utilizador = f.cod_utilizador;
            this.data_hora = f.data_hora;
        }
    }
}
#endregion

#region dataFoto
namespace Distrackting.DataLayer
{
    public class dataFoto
    {
        #region Methods
        public static void createFoto(Foto f)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                dbData.Fotos.InsertOnSubmit(f);
                dbData.SubmitChanges();

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        
        public static Foto getFoto(String url)
        {
            try
            {
                if (url == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Foto foto = (from f in dbData.Fotos
                             where f.url == url
                             select f).FirstOrDefault();
                return foto;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changeFoto(Foto f)
        {
            try
            {
                if (f == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Foto res = dbData.Fotos.FirstOrDefault(ft => ft.url == f.url);
                    if (res == null) return false;
                    else
                    {
                        res.setFoto(f);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Foto> getFotos()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Fotos;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Foto> getFotos(Expression<Func<Foto, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Fotos.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        
        public static Boolean deleteFoto(Foto f)
        {
            try{
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Fotos.Contains(f)){
                    Foto res = (from ft in dbData.Fotos
                                 where ft.url == f.url
                                 select ft).FirstOrDefault();
                    if (res==null)
                        return false;
                    else
                    {
                        dbData.Fotos.DeleteOnSubmit(f);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static int contarFotos()
        {
            dbDataDataContext dbData = new dbDataDataContext();
            try
            {
                return dbData.Fotos.Count();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion